package com.cw.lang.common.exception;

import com.cw.lang.common.log.TenantLog;

/**
 * Description: BizException
 *
 * @author chenw
 * @version 1.0
 * @since v1.0
 */
public class BizException extends AbstractExceptionWrapper {

    private static final long serialVersionUID = 3780394087516409112L;

    /**
     * 异常构造函数
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param params 描述中的占位符替换值
     */
    public BizException(String errorCode, String errorMessage, String... params) {
        super(format(errorMessage, (Object[]) params));
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        logger = TenantLog.getLogger(BizException.class);
        logger.error(format(errorMessage, (Object[]) params));
    }

    /**
     * 异常构造函数
     *
     * @param t 异常
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param params 描述中的占位符替换值
     */
    public BizException(Throwable t, String errorCode, String errorMessage, String... params) {
        super(format(errorMessage, (Object[]) params));
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        logger = TenantLog.getLogger(BizException.class);
        logger.error(t, format(errorMessage, (Object[]) params));
    }

    /**
     * 异常构造函数，为了兼容老的异常
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param t 异常
     */
    public BizException(String errorCode, String errorMessage, Throwable t) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        logger = TenantLog.getLogger(BizException.class);
        logger.error(t, errorMessage);
    }

    /**
     * 异常构造函数，为了兼容老的异常
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param t 异常
     */
    public BizException(String errorCode, String errorMessage, Class<?> clazz, Throwable t) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        logger = TenantLog.getLogger(clazz);
        logger.error(t, errorMessage);
    }

    /**
     * 异常构造函数
     *
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param params 描述中的占位符替换值
     */
    public BizException(String errorCode, String errorMessage, Class<?> clazz, String... params) {
        super(format(errorMessage, (Object[]) params));
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        logger = TenantLog.getLogger(clazz);
        logger.error(format(errorMessage, (Object[]) params));
    }

    /**
     * 异常构造函数
     *
     * @param t 异常
     * @param errorCode 异常code
     * @param errorMessage 异常描述
     * @param clazz 抛出异常的class类
     * @param params 描述中的占位符替换值
     */
    public BizException(Throwable t, String errorCode, String errorMessage, Class<?> clazz, String... params) {
        super(format(errorMessage, (Object[]) params));
        this.errorCode = errorCode;
        this.errorMessage = format(errorMessage, (Object[]) params);
        logger = TenantLog.getLogger(clazz);
        logger.error(t, format(errorMessage, (Object[]) params));
    }
}
